{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.5"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python395jvsc74a57bd0d21333ff62c43b8d3f22f2fffa3b7389f580599dfc1ab4fc74fc1532be50abca",
   "display_name": "Python 3.9.5 64-bit"
  },
  "metadata": {
   "interpreter": {
    "hash": "d21333ff62c43b8d3f22f2fffa3b7389f580599dfc1ab4fc74fc1532be50abca"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "123456\nHello World ~\n"
     ]
    }
   ],
   "source": [
    "class MyClass:\n",
    "    i = 123456\n",
    "    def fn(self):\n",
    "        return \"Hello World ~\"\n",
    "# 实例化MyClass\n",
    "myclass = MyClass()\n",
    "#访问属性和方法：\n",
    "print(myclass.i) \n",
    "print(myclass.fn())      "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "3.0 -4.5\n"
     ]
    }
   ],
   "source": [
    "# 类的构造方法\n",
    "class Complex:\n",
    "    def __init__(self,realpar,imagpart):\n",
    "        self.r = realpar\n",
    "        self.i = imagpart\n",
    "\n",
    "x = Complex(3.0,-4.5)\n",
    "print(x.r,x.i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "<__main__.Test object at 0x00000237FE666940>\n<class '__main__.Test'>\n"
     ]
    }
   ],
   "source": [
    "# self代表类的实例，而非类 \n",
    "# 类的方法与普通的函数只有一个特别的区别——它们必须有一个额外的第一个参数名称, 按照惯例它的名称是 self。\n",
    "\n",
    "class Test:\n",
    "    def prt(self):\n",
    "       print(self)\n",
    "       print(self.__class__)\n",
    "t = Test()\n",
    "t.prt()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "<__main__.Test object at 0x00000237FE395EE0>\n<class '__main__.Test'>\n"
     ]
    }
   ],
   "source": [
    "# 从执行结果可以很明显的看出，self 代表的是类的实例，代表当前对象的地址，而 self.class 则指向类。\n",
    "# self 不是 python 关键字，我们把他换成 runoob 也是可以正常执行的:\n",
    "class Test:\n",
    "    def prt(runoob):\n",
    "        print(runoob)\n",
    "        print(runoob.__class__)\n",
    "\n",
    "t = Test()\n",
    "t.prt()       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Jim 说： 我 10 岁了。\n"
     ]
    }
   ],
   "source": [
    "# 类的方法\n",
    "# 在类的内部，使用 def 关键字来定义一个方法，与一般函数定义不同，\n",
    "# 类方法必须包含参数 self, 且为第一个参数，self 代表的是类的实例。\n",
    "\n",
    "class people:\n",
    "    name = ''\n",
    "    age = 0\n",
    "    # 这个是私有属性，在类外部无法访问：\n",
    "    __weight = 0\n",
    "    def __init__(self,n,a,w):\n",
    "        self.name = n\n",
    "        self.age = a\n",
    "        self.__weight = w\n",
    "\n",
    "    def speak(self):\n",
    "        print(\"%s 说： 我 %d 岁了。\"%(self.name,self.age))\n",
    "\n",
    "p = people('Jim',10,30)\n",
    "p.speak()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}